<?php

	eb_include_class('EbTableSQLModel');

	class CategorieModel extends EbTableSQLModel
	{
		public function __construct()
		{			
			parent::init('vp_categorie',null,'ca_id');
		}
		
		public function getColumns()
		{
			return array(
				new EbTableColumn("ca_id","int",true,EB_PRIMARY_KEY,true),
				new EbTableColumn('urlname','varchar(128)',true,EB_UNIQUE_KEY),
				new EbTableColumn("name","varchar(64)")
			);
		}
				
		public function create()
		{
			if (parent::create())
			{
				$this->insert(array('name' => 'Commerces et magasins', 'urlname' => 'commerces-et-magasins'));
				$this->insert(array('name' => 'Art du mariage', 'urlname' => 'art-du-mariage'));
				$this->insert(array('name' => 'Bar - Cafés', 'urlname' => 'bar-cafes'));
				$this->insert(array('name' => 'Restaurants', 'urlname' => 'restaurants'));
				$this->insert(array('name' => 'Voyages', 'urlname' => 'voyages'));
				$this->insert(array('name' => 'Entreprises', 'urlname' => 'entreprises'));
			}
		}
		
		public function selectFromUrl($name)
		{
			$sql = 'SELECT vp_categorie.ca_id,vp_categorie.name ';
			$sql .= 'FROM vp_categorie ';
			$sql .= 'WHERE UPPER(vp_categorie.urlname) = UPPER(\''.$name.'\') LIMIT 1';
			
			return $this->execute($sql);
		}
		
		public function selectPrepared()
		{
			//$routes = EbModel::getModel('Core','UrlRoute')->selectByName('vpCategorie');
			//$route = $routes[0];
			
			$sql = 'SELECT vp_categorie.*,(SELECT COUNT(*) FROM vp_boutique WHERE vp_boutique.ca_id = vp_categorie.ca_id) AS boutiques FROM vp_categorie ';
		
			$cats = $this->execute($sql);
			
			foreach ($cats AS $cat)
			{
				$cat->url = '/categorie/'.$cat->urlname;
			}
			
			return $cats;
		}
	}